<?php
//用户咨询控制器
class Controller_WWW_GuiderChat extends Controller_WWW_Template
{
    public function __construct ($req, $res)
    {
        $this->template = "smarty:app/chat/chat";
        parent::__construct($req, $res);

    }

	//默认首页
    public function action_index ()
    {

    }

	//用户咨询页
	//$uid int 用户id
    public function action_user ($uid)
    {
        $user = User::getUserFromUid($uid);
        $userInfo = $user->getUserInfo();
//        var_dump($userInfo);
        $chatList = App_Chat::guiderInfoList($user,$this->_uid);
        $this->template->set_filename('smarty:www/guider/chat/chat');
        $this->template->set('chatList', $chatList);
        $this->template->set('chatUid', $uid);
        $this->template->set('userInfo', $userInfo);
    }

    public function action_refresh()
    {
        $this->_post = $this->request->post();
    	$uid = (int) Arr::get($this->_post, 'uid');
    	$limit = (int) Arr::get($this->_post, 'limit');
    	$offset = (int) Arr::get($this->_post, 'offset');
        $cache = Cache::instance('file');
        $key = $uid.'-'.$this->_uid;
        $unread = $cache->get($key);
        $unread['total'] = 0;
        $cache->set($key,$unread);
        $user     = User::getUserFromUid($uid);
        $chatList = App_Chat::guiderInfoList($user,$this->_uid,$offset,$limit);

        $html     = View::factory('smarty:www/guider/chat/records')
            ->set('chatList', $chatList)
            ->render();

        $ret = array('status' => 0, 'html' => $html);
        echo json_encode($ret);exit;
    }



	//最新消息
    public function action_newest ()
    {
        $this->_no_list_actions  = FALSE;
        $page = $this->request->query('page');
        $page = max(1, $page);
        parent::action_list($page);
    }

    public function list_get_data ($offset, $limit)
    {
        $list = App_Account::newChatMessages($offset, $limit);
        return $list;
    }

    public function actions_by_list_row (& $row, & $menus)
    {
        array_pop($menus);
        array_pop($menus);
        array_pop($menus);

        $menus[] = '<a href="/admin/chat/user/'.$row['uid'].'" target="_blank">进入聊天</a>';
    }

	//接受并处理提交到服务器的信息
    public function action_api ()
    {
        $this->_post = $this->request->post();
        $img = Arr::get($this->_post, 'img');
        $img = trim($img);
        $txt = Arr::get($this->_post, 'txt');
        $txt = trim($txt);
        if (! empty($txt))
        {
        	$txt = htmlspecialchars($txt);
        	$txt = nl2br($txt);
        }
        if(stripos($txt,'#') !== FALSE){
        	$search =array(
        			"#呵呵#", "#哈哈#","#吐舌#", "#啊#","#酷#","#怒#", "#开心#", "#汗#", "#泪#", "#黑线#",
        			"#鄙视#", "#不高兴#", "#真棒#", "#钱#", "#疑问#", "#阴脸#", "#吐#", "#咦#", "#委屈#",
        			"#花心#", "#呼~#", "#笑脸#", "#冷#", "#太开心#", "#滑稽#", "#勉强#", "#狂汗#", "#乖#",
        			"#睡觉#", "#惊哭#", "#生气#", "#惊讶#", "#喷#", "#爱心#", "#心碎#", "#玫瑰#", "#礼物#",
        			"#彩虹#", "#星星月亮#", "#太阳#", "#钱币#", "#灯泡#","#茶杯#", "#蛋糕#", "#音乐#",
        			"#haha#","#胜利#","#大拇指#", "#弱#","#OK#");
        	foreach($search as $k=>$v){
        		$k++;
        		$path="/resource/skin/face/tieba/".$k.".png";
        		$txt=str_replace($v, "<img src=\"$path\" />", $txt);
        	}
        }
        $toUid = Arr::get($this->_post, 'touid');
        $cache = Cache::instance('file');
        $key = $this->_uid.'-'.$toUid;
        $data = $cache->get($key);
        if(empty($data)){
            $data['total'] = 0;
            $data['msg'] = '';
            $data['time'] = time();
            $data['last'] = time();
        }
        //对方不在线上次输入和本次输入相差1h以上提醒
        $dif=time()-Arr::get($data,'time');
        $dif=abs($dif);
        if(!Util_User::is_online($toUid)&&$dif>3600){
            Util_Notice::notice_message($toUid,$this->_uid,'GUIDER');
        }
        if (! empty($img))
            $img = "<img src=\"$img\">";

        if (empty($txt) && empty($img))
            exit;

        $txt   = empty($img) ? $txt : $img;
        if ($toUid > 0)
        {
            $data['total'] += 1;
            $data['msg'] = $txt;
            $data['last'] = Arr::get($data,'time',time());
            $data['time'] = time();
            $cache->set($key,$data,86400*10);
            $user  = User::getUserFromUid($toUid);
            $guiderId = $this->_uid;
            $msgId = App_Chat::add($user, array('message' => $txt, 'fromUser' => 0,'guiderId'=>$guiderId));
        }
        else
        {
            $msgId = App_Chat::add($this->_user, array('message' => $txt, 'fromUser' => 1));
        }

       // $html  = App_Chat::buildMessageHtml($msgId);
            
        echo json_encode(array(
            'status' => 0,
        //    'html'   => $html,
        ));

        exit;
    }
}
